<?php

/**
 * configuracion actions.
 *
 * @package    tercermilenio
 * @subpackage configuracion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class configuracionActions extends sfActions {

    public function executeEdit(sfWebRequest $request) {
        $pathConfig = sfConfig::get('sf_app_config_dir');
        $file = '/app.yml';
        try {
            $loader = sfYaml::load($pathConfig . $file);
        } catch (InvalidArgumentException $e) {
            // an error occurred during parsing
            echo "Unable to parse the YAML string: " . $e->getMessage();
        }
        $this->configuracion = $loader;
        $this->form = new ConfiguracionForm();
    }

    public function executeUpdate(sfWebRequest $request) {
        //$this->productos = $this->getRoute()->getObject();
        $this->form = new ConfiguracionForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->redirect('configuracion/edit');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            //$notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

            $formValues = $form->getValues();
            //$productos = $form->save();
            //$this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $productos)));

            $pathConfig = sfConfig::get('sf_app_config_dir');
            $file = '/app.yml';

            try {
                $loader = sfYaml::load($pathConfig . $file);
            } catch (InvalidArgumentException $e) {
                // an error occurred during parsing
                $this->getUser()->setFlash('error', 'El archivo no ha podido ser cargado.', false);
                $this->redirect('configuracion/edit');
            } catch (Exception $e) {
                // an error occurred during parsing
                $this->getUser()->setFlash('error', 'El archivo no ha podido ser cargado.', false);
                $this->redirect('configuracion/edit');
            }

            $loader['all']['iva'] = $formValues['iva'];
            $loader['all']['descuento'] = $formValues['descuento'];
            $loader['all']['caja_valor_inicial_diario'] = $formValues['caja_inicial'];

            $dumper = new sfYamlDumper();
            $yaml = $dumper->dump($loader, 4);
            //try {
                
                $written = file_put_contents($pathConfig . $file, $yaml);
                //var_dump($written);exit();
                if($written!==false){
                $this->getUser()->setFlash('notice', 'Los valores han sido establecidos exitosamente.');
                $this->redirect('configuracion/edit');
                }else{
                    $this->getUser()->setFlash('error', 'Los valores no han podido ser guardados.Consulte al administrador del sistema.');
                
                $this->redirect('configuracion/edit');
                    //throw new Exception();
                }
            //} catch (Exception $e) {
                // an error occurred during parsing
                
            //}
        } else {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.');
        }
    }

}
